/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;

namespace FourRoads.Links.Controls
{
	/// <summary>
	/// Summary description for ResLinkDeleteCommentReason.
	/// </summary>
	public class ResLinkDeleteCommentReason : ResLinkBaseTemplatedWebControl
	{
		#region Child Controls
			TextBox _Reason;
			Button  _Cancel;
			Button  _Delete;
		#endregion

		public ResLinkDeleteCommentReason()
		{

		}

		protected override void AttachChildControls()
		{
			_Reason = FindControl("Reason") as TextBox;
			_Cancel = FindControl("Cancel") as Button;
			_Delete = FindControl("Delete") as Button;

			_Cancel.Click += new EventHandler(_Cancel_Click);
			_Delete.Click += new EventHandler(_Delete_Click);

			_Cancel.CausesValidation = false;
		}

		private void RedirectToOrginalPage()
		{
			string returnURL = Page.Request.QueryString["returnURL"];

			if (returnURL == null)
			{
				Page.Response.Redirect(SiteUrls.Instance().Home);
			}
			else
			{
				Page.Response.Redirect(Page.Server.UrlDecode(returnURL));
			}
		}

		private void _Cancel_Click(object sender, EventArgs e)
		{
			RedirectToOrginalPage();
		}

		private void _Delete_Click(object sender, EventArgs e)
		{
			if ( Page.IsValid )
			{
				ResLinkPermission.AccessCheck(ThisSection, ResLinkPermissionEnum.Moderate , CSContext.Current.User );

				//Get the correct link
				ResLinkLink lnk = ThisSection.FindLink(CSContext.Current.PostID);

				int comId =  CSContext.Current.GetIntFromQueryString("CommentID" , -1);
				ResLinkComments com = null;

				if (comId > -1)
				{
					foreach(ResLinkComments c in lnk.Comments)
					{
						if (c.PostID == comId)
						{
							com = c;
							break;
						}
					}

					if (lnk != null && com != null)
					{
						lnk.DeleteComment(ThisSection.SectionID , com , com.User.UserID  , _Reason.Text.Trim());
					}
				}
				RedirectToOrginalPage();
			}
		}
	}
}
